package com.igeek.web;

import com.igeek.domain.User;
import com.igeek.service.UserService;
import com.igeek.utils.MailUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;

/**
 * @author jerryHe
 * @create 2019-10-08 15:59
 */

@WebServlet("/user")
public class UserServlet extends BaseServlet {

    public void checkUsername(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        UserService service = new UserService();
        boolean isExist =  service.checkUsername(username);
        String jsonString = "{\"isExist\":"+isExist+"}";
        System.out.println(jsonString);
        response.getWriter().write(jsonString);
    }

    //http://localhost:8889/webstore/user?method=login
    public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //BeanUtils转换日期类型
        //1.创建DateConverter 对象
        DateConverter dateConverter = new DateConverter();
        //2.设置可以接收的日期格式
        dateConverter.setPatterns(new String[]{"yyyy-MM-dd","yyyy/MM/dd","yyyy-MM-dd HH:mm:ss"});
        //3.把创建的dataConverter注册到BeanUtils中
        ConvertUtils.register(dateConverter,Date.class);

        //使用beanutils获取页面所有参数
        User user = new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(user);
        //生成uuid
        String uuid = UUID.randomUUID().toString();
        user.setUid(uuid);

        //生成激活码
        String activeCode  = UUID.randomUUID().toString();
        user.setCode(activeCode);

        //调用service
        UserService service = new UserService();
        service.regist(user);
        //*发送内容的格式：超链接，包含随机数
        //http://localhost:8888/webstore/user?method=active?activeCode=XXXX*//*
        String content = "<a href='http://localhost:8888/webstore/user?method=active&activeCode="+activeCode+"'>点击链接激活邮件</a>";
        //发送邮件
        MailUtils.sendMail(user.getEmail(),"激活邮件",content);
        //跳转到页面
        request.getRequestDispatcher("registerSuccess.jsp").forward(request,response);
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            System.out.println(user);
            //调用service
            UserService service = new UserService();
            if(service.login(user)){
                request.getRequestDispatcher("index.jsp").forward(request,response);
            }else{
                response.sendRedirect("login.jsp");
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }



    public void active(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接收激活码随机数
        String activeCode = request.getParameter("activeCode");
        UserService service = new UserService();
        //1.判断激活码是否一致
        boolean flag = service.checkActiveCode(activeCode);
        if(flag){
            //2.修改状态
            service.updateActiveCode(activeCode);
            //激活成功，跳转到登录页面
            response.sendRedirect("login.jsp");
        }else{
            //激活失败，跳转到注册页面
            response.sendRedirect("register.jsp");
        }
    }
}
